<?php
declare (strict_types=1);

namespace app\school\service\school\admin;

use app\common\service\BaseService;
use app\school\logic\school\admin\SchoolDomain as SchoolDomainLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

class SchoolDomain extends BaseService
{

    /**
     * @Inject()
     *
     * @var SchoolDomainLogic
     */
    protected $schoolDomainLogic;

    /**
     * 获取信息
     *
     * @return Response
     */
    public function info()
    {
        $mhmId = (int)request()->mhm_id;

        // 当前生效域名
        $effectivePcDomain = $this->schoolDomainLogic->getEffectivePcDomain($mhmId);
        // 机构的域名: 最新一条通过的记录
        $info = $this->schoolDomainLogic->getInfoByMhmId($mhmId, -1);
        $data = [
            // 当前有效域名
            'effective_domain'  => $effectivePcDomain,
            // 当前使用的模式
            'node'              => $info->getAttr('node'),
            // 当前申请的审核状态
            'status'            => $info->getAttr('status'),
            // 当前使用的二级域名前缀
            'sub_domain_prefix' => $info->getAttr('node') === 1 ? $info->getAttr('sub_domain_prefix') : $this->schoolDomainLogic->getSubDomainPrefix($mhmId),//$this->schoolDomainLogic->getSubDomainPrefix($mhmId),
            // 二级域名后缀
            'sub_domain_suffix' => $this->schoolDomainLogic->getPcSubDomainSuffix(),
            // 自定义域名
            'domain'            => $info->getAttr('node') === 2 ? $info->getAttr('domain') : $this->schoolDomainLogic->getLatestDomain($mhmId),
            // PC域名的CNAME解析值
            'cname'             => $this->schoolDomainLogic->getPcCname($mhmId),
            // 驳回理由
            'audit_reason'      => $info->getAttr('audit_reason')
        ];

        // // 子域名
        // $info->sub_domain = $this->schoolDomainLogic->getPcSubDomain($mhmId, $info->sub_domain_prefix);

        return $this->parseToData($data, 1);
    }

    /**
     * 检测cname状态
     *
     * @return Response
     */
    public function checkCname()
    {
        $mhmId  = (int)request()->mhm_id;
        $domain = Request::post('domain', '');
        $status = 0;
        $count  = 0;

        while ($count < 5 && $status === 0) {
            $status = $this->schoolDomainLogic->getCnameStatus($domain, $mhmId);
            if ($status === 0) {
                sleep(2);
            }
            $count++;
        }

        return $this->parseToData(['status' => $status], 1);
    }

    /**
     * 列表
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $where = [];

        $data = $this->schoolDomainLogic->getList($where);

        // 定义字段
        $keyList = [
            'school_title'     => ListItem::make()->title('机构'),
            'effective_domain' => ListItem::make()->title('当前生效域名'),
            'domain'           => ListItem::make('custom')->title('申请域名'),
            'create_time'      => ListItem::make()->title('申请时间'),
            // 'status'       => ListItem::make('custom')->title('当前自定义域名状态'),
        ];
        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 创建
     *
     * @return Response
     */
    public function created(): Response
    {
        $data = Request::post();
        $this->schoolDomainLogic->created($data);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 审核
     *
     * @param int $id
     * @return Response
     */
    public function audit(int $id)
    {
        $status      = Request::post('status/d', 0);
        $mhmId       = Request::post('mhm_id/d', 0);
        $auditReason = Request::post('audit_reason/s');

        $this->schoolDomainLogic->doAudit($id, $mhmId, $status, $auditReason);

        return $this->parseToData([], 1, '操作成功');
    }
}